//
//  ViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "ViewController.h"
#import "Music.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>

@interface ViewController ()
@property(nonatomic, strong)UILabel *netErrorLabel;
@property(nonatomic, assign)BOOL isMove;

@end

@implementation ViewController

-(void)dealloc
{
    //移除消息监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -懒加载
-(NSMutableArray *)urlArray
{
    if (!_urlArray) {
        _urlArray = [NSMutableArray array];
    }
    return _urlArray;
}

-(NSMutableArray *)infoArray
{
    if (!_infoArray) {
        _infoArray = [NSMutableArray array];
    }
    return _infoArray;
}

-(UILabel *)netErrorLabel
{
    if (!_netErrorLabel) {
        _netErrorLabel = [[UILabel alloc] initWithFrame:FitRectMake(0, -40, 375, 40)];
        _netErrorLabel.backgroundColor = [UIColor blackColor];
        _netErrorLabel.textColor = [UIColor whiteColor];
        _netErrorLabel.text = @"网络连接失败, 请检查网络!";
        [self.view addSubview:_netErrorLabel];
    }
    return _netErrorLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.tabBarController.view addSubview:];
    //设置初始图片
    
    if (![PlayerButton shareButton].imageView.image) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"picURL"]) {
            [[PlayerButton shareButton] sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"picURL"]] forState:UIControlStateNormal];
        }
        else{
            [[PlayerButton shareButton] setImage:[UIImage imageNamed:@"placeholderZh"] forState:UIControlStateNormal];
        }
        
    }
    [PlayerButton shareButton].imageView.layer.cornerRadius = 35;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 70)];
        imageView.image = [UIImage imageNamed:@"playerButtonBackGround"];
        [[PlayerButton shareButton] addSubview:imageView];
        [[PlayerButton shareButton] sendSubviewToBack:imageView];
    });
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netError) name:@"net" object:nil];
}


#pragma mark 网络出错通知方法
-(void)netError
{
    [UIView animateWithDuration:.5 animations:^{
        self.netErrorLabel.frame = FitRectMake(0, 0, 375, 40);
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1200000.0 * NSEC_PER_USEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:.5 animations:^{
                self.netErrorLabel.frame = FitRectMake(0, -40, 375, 40);
            } completion:^(BOOL finished) {
                //                [self.netErrorLabel removeFromSuperview];
            }];
        });
        
    }];
    
    [self stopRefresh];
}

#pragma mark 循环加载数据
-(void)circulationGetData
{
    [AFTools getDataByURLString:_urlArray[0] WithDataBlock:^(id data) {
        if (_urlArray.count != 1) {
            [_urlArray removeObjectAtIndex:0];
            [self.infoArray addObject:data];
            [self circulationGetData];
        }
        else{
            [self.infoArray addObject:data];
            [_urlArray removeLastObject];
            [self infoDidLoadFinish];
        }
    }];
}

#pragma mark 解析完成调用的方法
-(void)infoDidLoadFinish
{
    
}

#pragma mark 断网之后停止刷新加载
-(void)stopRefresh
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
